import { reactive } from 'vue'

export const eventBus = reactive({
    // 存储事件处理函数
    events: {},
    
    // 订阅事件
    on(eventName, callback) {
        if (!this.events[eventName]) {
            this.events[eventName] = []
        }
        this.events[eventName].push(callback)
    },
    
    // 触发事件
    emit(eventName, data) {
        if (this.events[eventName]) {
            this.events[eventName].forEach(callback => callback(data))
        }
    },
    
    // 取消订阅
    off(eventName, callback) {
        if (this.events[eventName]) {
            if (callback) {
                this.events[eventName] = this.events[eventName].filter(cb => cb !== callback)
            } else {
                delete this.events[eventName]
            }
        }
    }
})